#ifndef INC_TOOLMAN_H
#define INC_TOOLMAN_H

#include "../view/Palette.h"
#include "../view/PaletteItemLr.h"

#include <vector>
#include <boost/utility.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

class QWidget;
class Tool;
class ToolPIL;
class DiagramMan;

/**
 * Creates and manages Tools.
 *
 * Remember to destroy this BEFORE DESTROYING the DiagramMan.
 */
class ToolMan
{
	public:
		ToolMan(DiagramMan& dman, QWidget* paletteParent);

		/**
		 * @returns a ToolMan-owned pointer to a QWidget that will display a
		 * palette of tool buttons.
		 */
		QWidget* palette();

	private:

		/**
		 * This class manages a single Tool and listens to relevant events. It's
		 * just a slave to the ToolMan, so it's really just a middle-manager.
		 */
		class ToolPHB : public PaletteItemLr, boost::noncopyable
		{
			public:
				/**
				 * Takes ownership of tool.
				 */
				ToolPHB(DiagramMan& dman, Tool *tool);
				~ToolPHB();
	
				void toggled(bool state);

			private:

				DiagramMan& _dman;

				boost::scoped_ptr<Tool> _tool;
		};

		// Note: this is a QWidget. It is not (necessarily) heap-allocated. This
		// is OK as long palette() callers behave.
		Palette _palette;

		std::vector<boost::shared_ptr<ToolPHB> > _tools;

		// A ToolPHB will own the Tool*, don't keep it!
		void addTool(DiagramMan& dman, const std::string& name, Tool* tool);
};

#endif // INC_TOOLMAN_H

